package com.example.bloodbankapplication.dialogfragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.bloodbankapplication.R;
import com.example.bloodbankapplication.entity.CitiesModel;
import com.example.bloodbankapplication.fragment.DonorRegistrationFragment;
import com.example.bloodbankapplication.listener.MyListener;

public class CitiesDialogFragment extends BaseDialogFragment implements OnItemClickListener {
	
	private ArrayList<CitiesModel> list;
	private MyListener listener;
	
	private ListView lv;
	public CitiesDialogFragment(ArrayList<CitiesModel> citiesList) {
	
		list = citiesList;
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		
		//listener = (MyListener) getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.states_dialog, null);
				
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		lv = (ListView) getView().findViewById(R.id.listview);
		getDialog().setTitle(getResources().getString(R.string.statsTitle));
		Collections.sort(list, new ListComparator());
		lv.setAdapter(new DataDisplayAdapter(list));
		lv.setOnItemClickListener(this);
		
	}
	
	class ListComparator implements Comparator<CitiesModel>,Serializable
	{
	public int compare(CitiesModel o1, CitiesModel o2)
	{

	return o1.getName().compareTo(o2.getName());

	}
	}
	
class DataDisplayAdapter extends BaseAdapter{
		
		//scope of global variable should be entire class
		private ArrayList<CitiesModel> citiesList;

		public DataDisplayAdapter(ArrayList<CitiesModel> list) {
			// scope of local variable should be with in the block;
			citiesList = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return citiesList.size();
		}

		@Override
		public Object getItem(int arg3) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}
		
		class ViewHolder {
			
			public TextView name,phno;
			public ImageView img;
		}

		@Override
		public View getView(int position, View v, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ViewHolder holder;
			if(v == null){
				holder = new ViewHolder();
				LayoutInflater inflater = getActivity().getLayoutInflater();
				v = inflater.inflate(R.layout.datadisplay, null);
				holder.name = (TextView) v.findViewById(R.id.name);
				holder.phno = (TextView) v.findViewById(R.id.number);
				holder.img = (ImageView) v.findViewById(R.id.imageview);
				holder.phno.setVisibility(View.GONE);
				holder.img.setVisibility(View.GONE);
				v.setTag(holder);
				
			}else{
				 holder = (ViewHolder) v.getTag();
			}
			
			
			CitiesModel model = citiesList.get(position);
			holder.name.setText(model.getName());
			holder.name.setTextColor(Color.RED);
			
			return v;
		}
		
	}


@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {

	CitiesModel model = list.get(position);
	String cityName = model.getName();
	DonorRegistrationFragment fragment = (DonorRegistrationFragment) getFragmentManager().findFragmentById(R.id.fragment);
	fragment.getCitiesData(cityName);
	getDialog().dismiss();
}
	

}
